What is jasmine-core?
The jasmine-core package is the core framework for Jasmine, a behavior-driven development framework for testing JavaScript code. It does not rely on browsers, DOM, or any JavaScript framework, making it suitable for websites, Node.js projects, or anywhere that JavaScript can run.
What are jasmine-core's main functionalities?
Writing Test Suites
This feature allows you to group related specs (tests) together. The 'describe' function is for grouping, and 'it' is for individual specs.
describe('A suite is just a function', function() {
var a;
it('and so is a spec', function() {
a = true;
expect(a).toBe(true);
});
});
Matchers
Matchers are used to implement a boolean comparison between the actual value and the expected value. The 'toBe' matcher is one of the simplest matchers.
describe('The 'toBe' matcher compares with ===', function() {
it('and has a positive case', function() {
expect(true).toBe(true);
});
it('and can have a negative case', function() {
expect(false).not.toBe(true);
});
});
Setup and Teardown
Setup and teardown functions like 'beforeEach' and 'afterEach' are used to perform actions before and after each spec in a suite, respectively.
describe('A spec', function() {
beforeEach(function() {
this.value = 0;
});
afterEach(function() {
this.value = null;
});
it('can use the `this` to share state', function() {
expect(this.value).toEqual(0);
this.value += 1;
});
it('prevents test pollution by having an isolated `this` per spec', function() {
expect(this.value).toEqual(0);
});
});
Spies
Spies are used to track calls to functions and all arguments of those calls. They can be used to stub any function and track its usage and calls.
describe('A spy', function() {
var foo, bar = null;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
};
spyOn(foo, 'setBar');
foo.setBar(123);
foo.setBar(456, 'another param');
});
it('tracks that the spy was called', function() {
expect(foo.setBar).toHaveBeenCalled();
});
});
Asynchronous Support
Jasmine supports asynchronous testing by using the 'done' function to signal that an async function has completed before moving on to the next spec.
describe('Async spec', function() {
var value;
beforeEach(function(done) {
setTimeout(function() {
value = 0;
done();
}, 1);
});
it('should support async execution of test preparation and expectations', function(done) {
value++;
expect(value).toBeGreaterThan(0);
done();
});
});
Other packages similar to jasmine-core
mocha
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple. It's often compared to Jasmine, with the main difference being that Mocha allows for more flexibility in terms of assertion libraries, mock/stub utilities, and reporting.
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works out of the box for any React project but can be used more broadly. It includes its own assertion library and has a more powerful mocking library compared to Jasmine.
chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any JavaScript testing framework. It's often used in conjunction with Mocha and provides a more expressive syntax and additional assertions over Jasmine's built-in matchers.
ava
AVA is a test runner for Node.js with a concise API, detailed error output, and process isolation that lets you develop with confidence. It's different from Jasmine in that it runs tests concurrently, which can be a significant advantage for testing I/O heavy operations.
A JavaScript Testing Framework
Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus it's suited for websites, Node.js projects, or anywhere that JavaScript can run.
Upgrading from Jasmine 3.x? Check out the upgrade guide.
Contributing
Please read the contributors' guide.
Installation
There are several different ways to install Jasmine, depending on your
environment and how you'd like to use it. See the Getting Started page
for details.
Usage
See the documentation site,
particularly the Your First Suite tutorial
for information on writing specs, and the FAQ.
Supported environments
Jasmine tests itself across popular browsers (Safari, Chrome, Firefox, and
Microsoft Edge) as well as Node.
Environment | Supported versions |
---|
Node | 12.17+, 14, 16, 18 |
Safari | 14-16 |
Chrome | Evergreen |
Firefox | Evergreen, 91, 102 |
Edge | Evergreen |
For evergreen browsers, each version of Jasmine is tested against the version of the browser that is available to us
at the time of release. Other browsers, as well as older & newer versions of some supported browsers, are likely to work.
However, Jasmine isn't tested against them and they aren't actively supported.
To find out what environments work with a particular Jasmine release, see the release notes.
Maintainers
Maintainers Emeritus
Copyright (c) 2008-2019 Pivotal Labs
Copyright (c) 2008-2024 The Jasmine developers
This software is licensed under the MIT License.